<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
	include('../../includes/classes/core.php');
	$action = $_REQUEST['act'];
	
	switch ($action) {
	    case 'get_edit_page':
	        $data[error]='';
            $data[page]= '
	        
		<div id="dialog-form">	
			<fieldset>
	          <legend>'.$_REQUEST[id].'</legend>	        
			    <div class="inner-table">
				    <div id="dt_example" class="ex_highlight_row">
				        <div id="container" class="overhead_container">
				        	<div id="button_area" class="inner_buttons">							
							</div>
				            <div id="dynamic">
				                <table class="display" id="inner_table">
                                    <thead>
                                        <tr id="datatable_header">
                                            <th>ID</th>
	                                        <th style="width: 141px;">დრო</th> 
                                            <th style="width: 92px;">ქმედება</th>       
                                            <th style="width: 150px;">მომხმარებელი</th>
                                            <th style="width: 100%;">ცხრილი</th>
                                            <th style="width: 150px;">ველი</th> 
                                            <th style="width: 150px;">ძველი მნიშვნელობა</th>
                                            <th style="width: 150px;">ახალი მნიშვნელობა</th>                                         
                                        </tr>
                                    </thead>
                                    <thead>
                                        <tr class="search_header">
                                            <th class="colum_hidden">
                                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
                                            </th>
                                            <th>
                                                <input type="text" name="search_type" value="ფილტრი" class="search_init"  />
                                            </th>
                                            <th>
                                                <input type="text" name="search_type" value="ფილტრი" class="search_init"  />
                                            </th>
                
                                            <th>
                                                <input type="text" name="search_type" value="ფილტრი" class="search_init" />
                                            </th>
	                                        <th>
                                                <input type="text" name="search_type" value="ფილტრი" class="search_init" />
                                            </th>
                                             <th>
                                                <input type="text"  name="search_type" value="ფილტრი" class="search_init"  />
                                            </th>    
                                            <th>
                                                <input type="text" name="search_code" value="ფილტრი"  class="search_init" />
                                            </th>                            
                                            <th>
                                               <input type="text"  name="search_model" value="ფილტრი" class="search_init" />                              
                                            </th>                      
                                        </tr>
                                    </thead>                       
                                    <tfoot>
                                        <tr>
                                            <th>&nbsp;</th>
	                                        <th>&nbsp;</th>    
                                            <th>&nbsp;</th>
                                            <th>&nbsp;</th>  
                                            <th>&nbsp;</th>                         
                                            <th>&nbsp;</th>
                                            <th>&nbsp;</th>
                                            <th>&nbsp;</th>
                                        </tr>
                                    </tfoot>
				                </table>
				            </div>
				        </div>
				    </div>
				</div>
			</fieldset>
	    </div>';
            echo json_encode( $data );
	        break;
	    case 'get_list':
    	    $count = $_REQUEST['count'];
    	    
    	    $rResult = mysql_query(" 
                        SELECT     CONCAT(row_id,'~^~',`date`) as id,
                        		  `date`as date, 	 
                        		  `persons`.`name`, 
                        		  CONCAT(IFNULL(`row_name`.t_rename,`logs`.`table`),' (',row_id,')') AS latter, 
                        		  IF(`event`='insert','დამატება','განახლება')    	                            	        
                        
                        FROM `logs`
                        LEFT JOIN users ON users.id=user_id
                        LEFT JOIN persons ON persons.id=users.person_id
                        LEFT JOIN row_name ON `logs`.`table`=row_name.`table` AND `logs`.collumn=row_name.`name`
                        where   `logs`.new_value != IFNULL(`logs`.old_value ,'~^~') AND row_name.actived=1  AND DATE(date)=DATE('$_REQUEST[date]') AND (`logs`.`table`='a_letter' OR `logs`.`table`='d_letter')    	        
                        GROUP BY  date, row_id
    	                HAVING LENGTH(latter)
    	        UNION ALL
                    	SELECT  CONCAT(row_id,'~^~',`date`) as id,
                    		   `date`, 	 
                    		   `persons`.`name`, 
                    			COALESCE(CONCAT('ა-',warehouse.a_letter_id, ' ', row_name.t_rename),CONCAT('დ-',warehouse.d_letter_id,' ',row_name.t_rename),CONCAT('R-',warehouse.id,' ',row_name.t_rename)) AS latter,
    	                        IF(`event`='insert','დამატება','განახლება')    	                            	        
                    	
                    	FROM `logs`
                    	LEFT JOIN users ON users.id=user_id
                    	LEFT JOIN persons ON persons.id=users.person_id
                        LEFT JOIN warehouse_detail ON warehouse_detail.id=row_id
                        LEFT JOIN warehouse ON warehouse.id=warehouse_detail.warehouse_id
                    	LEFT JOIN row_name ON `logs`.`table`=row_name.`table` AND `logs`.collumn=row_name.`name`
                    	where   `logs`.`table`='warehouse_detail' AND DATE(date)=DATE('$_REQUEST[date]') AND 	`logs`.new_value != IFNULL(`logs`.old_value ,'~^~') AND row_name.actived=1                
                    	GROUP BY  date, warehouse.id
    	                HAVING LENGTH(latter)
    	          UNION ALL
    	               SELECT     CONCAT(row_id,'~^~',`date`) as id,
			                     `date`as d, 	 
			                     `persons`.`name`, 
			                     CONCAT(IFNULL(`row_name`.t_rename,`logs`.`table`),' (',row_id,')') AS latter, 
			                     IF(`event`='insert','დამატება','განახლება')
                       FROM `logs`
                        LEFT JOIN users ON users.id=user_id
                        LEFT JOIN persons ON persons.id=users.person_id
                        LEFT JOIN row_name ON `logs`.`table`=row_name.`table` AND `logs`.collumn=row_name.`name`
                            JOIN warehouse on warehouse.id=row_id AND ISNULL(warehouse.a_letter_id) AND ISNULL(warehouse.d_letter_id)
                        where  `logs`.new_value != IFNULL(`logs`.old_value ,'~^~') AND row_name.actived=1  AND DATE(date)=DATE('$_REQUEST[date]') AND (`logs`.`table`='warehouse')
    	                GROUP BY  date, warehouse.id
    	                HAVING LENGTH(latter)
                        ORDER BY date DESC;           
");
    	    $output['aaData']=array();
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{

				$output['aaData'][] = $aRow;
			}
			
			
			echo json_encode( $output );

	        break;

        case 'get_list1':
            $count = $_REQUEST['count'];           
            $arr =  preg_split("/[,~^~]+/",$_REQUEST[id]);
            	
         $rResult = mysql_query("
             
    SELECT  'id',
            `date`,
             IF(`event`='insert','დამატება','განახლება') ,  
			`persons`.`name`,
			IFNULL(`row_name`.t_rename,`logs`.`table`),           
			IFNULL(`row_name`.`rename`,`logs`.collumn) AS veli,
            IFNULL(CASE  `logs`.collumn 
                        WHEN 'client_object_id' THEN (SELECT CONCAT(client.`name`,'-',client_objects.`name`) FROM client_objects	
            			JOIN client ON client_objects.client_id=client.id		 WHERE client_objects.id = `logs`.old_value LIMIT 1)
                        WHEN 'receiver'	 THEN (SELECT `name` FROM `persons` WHERE 	id=`logs`.old_value LIMIT 1)
            			WHEN 'receiver1' THEN (SELECT `name` FROM `persons` WHERE 	id=`logs`.old_value LIMIT 1)
                        WHEN 'requester' THEN (SELECT `name` FROM `client_object_persons` WHERE id=`logs`.old_value LIMIT 1)
            			WHEN 'source' 	 THEN (SELECT `name` FROM request_sources WHERE id=`logs`.old_value LIMIT 1)
            			WHEN 'car_id' 	 THEN (SELECT `name` FROM object WHERE 	id=`logs`.old_value LIMIT 1)
                        WHEN 'type' 	 THEN IF(`logs`.old_value=2,'საგარანტიო','ჩვეულებრივი')
            			WHEN 'responsible_person' THEN (SELECT `name` FROM `persons` WHERE 	id=`logs`.old_value LIMIT 1)
            			WHEN 'priority' THEN (SELECT `name` FROM `priority_catalog` WHERE 	id=`logs`.old_value LIMIT 1)
                        WHEN 'prod_id' THEN (SELECT `name` FROM `production` WHERE 	id=`logs`.old_value LIMIT 1)
            			WHEN 'producing_status' THEN
            					CASE  `old_value` WHEN 0 THEN 'მიღებული&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&nbsp;<img height=17 width=17 src=\"media/images/unread.png\">'
            								      WHEN 1 THEN 'მიღებული&emsp;&emsp;&nbsp;&emsp;&emsp;&emsp;&emsp;<img height=17 width=17 src=\"media/images/read.png\">'
            								      WHEN 2 THEN 'წარმ. დასრულებულია&nbsp;<img height=17 width=17 src=\"media/images/greenflag.png\">'
            					END 	
            				  WHEN 'status' 					THEN
                        IF(`logs`.`table`='warehouse',
            				(SELECT `name` FROM `warehouse_status` WHERE 	id=`logs`.old_value LIMIT 1),
                            (SELECT `name` FROM `request_status` WHERE 		id=`logs`.old_value LIMIT 1))
            				WHEN 'user_id' 					  THEN 				persons.`name`
            				WHEN 'actived' 					  THEN
            				CASE `logs`.old_value WHEN 1 THEN 'აქტიური' WHEN 0 THEN 'წაშლილი' END
            END,`logs`.old_value) AS `old_value`,
			IFNULL(CASE  `logs`.collumn 
                        WHEN 'client_object_id' THEN (SELECT CONCAT(client.`name`,'-',client_objects.`name`) FROM client_objects	
            			JOIN client ON client_objects.client_id=client.id		 WHERE client_objects.id = `logs`.new_value LIMIT 1)
                        WHEN 'receiver'	 THEN (SELECT `name` FROM `persons` WHERE 	id=`logs`.new_value LIMIT 1)
            			WHEN 'receiver1' THEN (SELECT `name` FROM `persons` WHERE 	id=`logs`.new_value LIMIT 1)
                        WHEN 'prod_id'   THEN (SELECT `name` FROM `production` WHERE 	id=`logs`.new_value LIMIT 1)
                        WHEN 'requester' THEN (SELECT `name` FROM `client_object_persons` WHERE id=`logs`.new_value LIMIT 1)
            			WHEN 'source' 	 THEN (SELECT `name` FROM request_sources WHERE id=`logs`.new_value LIMIT 1)
            			WHEN 'car_id' 	 THEN (SELECT `name` FROM object WHERE 	id=`logs`.new_value LIMIT 1)
                        WHEN 'type' 	 THEN IF(`logs`.new_value=2,'საგარანტიო','ჩვეულებრივი')
            			WHEN 'responsible_person' THEN (SELECT `name` FROM `persons` WHERE 	id=`logs`.new_value LIMIT 1)
            			WHEN 'priority' THEN (SELECT `name` FROM `priority_catalog` WHERE 	id=`logs`.new_value LIMIT 1)
            			WHEN 'producing_status' THEN
            					CASE  `new_value` WHEN 0 THEN 'მიღებული&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&nbsp;<img height=17 width=17 src=\"media/images/unread.png\">'
            								      WHEN 1 THEN 'მიღებული&emsp;&emsp;&nbsp;&emsp;&emsp;&emsp;&emsp;<img height=17 width=17 src=\"media/images/read.png\">'
            								      WHEN 2 THEN 'წარმ. დასრულებულია&nbsp;<img height=17 width=17 src=\"media/images/greenflag.png\">'
            					END 	
            				  WHEN 'status' 					THEN
                        IF(`logs`.`table`='warehouse',
            				(SELECT `name` FROM `warehouse_status` WHERE 	id=`logs`.new_value LIMIT 1),
                            (SELECT `name` FROM `request_status` WHERE 		id=`logs`.new_value LIMIT 1))
            				WHEN 'user_id' 					  THEN 				persons.`name`
            				WHEN 'actived' 					  THEN
            				CASE `logs`.new_value WHEN 1 THEN 'აქტიური' WHEN 0 THEN 'წაშლილი' END
            END,`logs`.new_value) AS `new_value`             
	FROM `logs`
    LEFT JOIN users ON users.id=user_id
	LEFT JOIN persons ON persons.id=users.person_id
    LEFT JOIN row_name ON `logs`.`table`=row_name.`table` AND `logs`.collumn=row_name.`name`
    where  `date`='$arr[1]' -- AND row_id='$arr[0]' 
             AND  `logs`.new_value != IFNULL(`logs`.old_value ,'~^~') AND row_name.actived=1       
    ORDER BY row_id, veli;        
                  
");
            	
            while ( $aRow = mysql_fetch_array( $rResult ) )
            {
        
                $output['aaData'][] = $aRow;
            }
            	
            echo json_encode( $output );
        
            break;
	        
	    default:
	       	echo "null";
	}
?>